﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;


namespace EnterpriseFund.DAL
{
    public class QueryAllowanceForecast
    {
        public QueryAllowanceForecast() { }

        public DataTable RunProcedure(string storedProcName, IDataParameter[] parameters)
        { 
            return DbHelperSQL.RunProcedure(storedProcName,parameters,"tax",0).Tables[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string sql)
        {
            DbHelperSQL.ExecuteSqlGet("", "");
            return DbHelperSQL.Query(sql).Tables[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string tableOrViewName, int Top = 0, string strWhere = "", string filedOrder = "")
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append("* FROM " + tableOrViewName + " ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string tableOrViewName,string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + tableOrViewName);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataTable GetListByPage(string tableOrViewName, string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FYear desc");
            }
            strSql.Append(")AS Row, T.*  from " + tableOrViewName + " T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }


        public int ExcSQL(string sql)
        {
            return DbHelperSQL.ExecuteSql(sql);
        }
    }
}
